<template>
	<view class="my-4 text-base">
		<uni-title type="h4" title="统计数据来源：全方案"></uni-title>
		<view class="text-base mb-4">推荐方案：【初选/7列选号池/过滤2】、【初选/前后区选号池/过滤3】、【优选/7列选号池/过滤2】、【优选/前后区选号池/过滤3】中奖率最佳。</view>
		<view class="my-2">
			<button size="mini" type="primary" @click="copyBigStat()">复制</button>
			<button size="mini" @click="resetBigStat()" class="mx-1">重置</button>
		</view>
		<view class="lh-lg">
			<uni-title type="h3" title="方案排行榜"></uni-title>
			<view v-for="(item, k, idx) of paramRank" :key="k" class="text-base">
				<uni-title type="h4" :title="`#${idx+1} ${item.title}（${item.winCount} 次）`"></uni-title>
				<view class="flex items-center mb-1">
					<uni-fav :checked="favCheckList[idx]" @click="onFavClick(idx)" class="me-1" />
					<uni-fav :checked="recommendCheckList[idx]" @click="onRecommendClick(idx)"
						:contentText="{contentDefault: '精华',contentFav: '已加精'}" />
				</view>
				<view>
					<view v-for="(item2, idx2) of item.prizeInfo" :key="idx2">
						{{item2.title + '：' + item2.count+' 次'}}
					</view>
				</view>
			</view>
		</view>
		<uni-title type="h3" title="奖项排行榜" class="mt-4"></uni-title>
		<view class="lh-lg" v-for="(item, k, idx) of prizeRank" :key="idx">
			<uni-title type="h4" :title="`#${idx+1} 等奖（${item.winCount} 次）`"></uni-title>
			<view v-for="(item2, idx2) of item.paramInfo" :key="idx2">{{ item2.title + '：'+item2.count+' 次'}}</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		prize_info
	} from '@/config/dlt-config';
	import {
		copyText
	} from '@/utils/Utils';
	import DltTplModel from '@/model/DltTplModel';
	import FsDriver from '@/model/FsDriver';

	const fsDriver = new FsDriver();
	const dltTplModel = new DltTplModel();

	const allParams = dltTplModel.getAllTplParams();

	const paramRank = ref({
		CF1: {
			title: '初选/7列选号池/过滤2',
			param: {},
			winCount: 0,
			prizeInfo: {
				prize1: {
					title: '一等奖',
					count: 0,
				}
			}
		}
	});

	const prizeRank = ref({
		prize1: {
			title: '一等奖',
			winCount: 0,
			paramInfo: {
				CF1: {
					title: '初选/7列选号池/过滤2',
					count: 0,
				}
			}
		}
	})

	let favList = [];
	const favCheckList = ref(new Array(allParams.length).fill(false));
	const onFavClick = (index) => {
		favCheckList.value[index] = !favCheckList.value[index];
		favList = [];
		favCheckList.value.forEach((v, i) => {
			if (v) {
				favList.push(allParams[i].id);
			}
		})
		fsDriver.save('fav-params', JSON.stringify(favList));
	}

	let recommendList = [];
	const recommendCheckList = ref(new Array(allParams.length).fill(false));
	const onRecommendClick = (index) => {
		recommendCheckList.value[index] = !recommendCheckList.value[index];
		recommendList = [];
		recommendCheckList.value.forEach((v, i) => {
			if (v) {
				recommendList.push(allParams[i].id);
			}
		})
		fsDriver.save('recommend-params', JSON.stringify(recommendList));
	}

	const loadCheckList = () => {
		favCheckList.value=[];
		recommendCheckList.value=[];
		let favData = JSON.parse(fsDriver.get('fav-params') || '[]');
		let recData = JSON.parse(fsDriver.get('recommend-params') || '[]');
		allParams.forEach(v=>{
			favCheckList.value.push(favData.includes(v.id));
			recommendCheckList.value.push(recData.includes(v.id));
		})
	}

	const copyBigStat = () => {
		let arr = [];
		for (let k in paramRank.value) {
			arr.push(paramRank.value[k].title + '：' + paramRank.value[k].winCount+'次');
		}
		for (let k in prizeRank.value) {
			arr.push(prizeRank.value[k].title + '：' + prizeRank.value[k].winCount+'次');
		}

		copyText(arr.join('\n'));
		uni.showToast({
			title: '复制成功',
			icon: 'success'
		})
	}

	const getBigStat = () => {
		let dataStr = fsDriver.get('big-stat');
		if (dataStr) {
			return JSON.parse(dataStr);
		}
		let data = {
			paramRank: {},
			prizeRank: {}
		};
		allParams.forEach((param, i) => {
			param.title = dltTplModel.getParamText(param);
			data.paramRank[param.id] = {
				title: param.title,
				param,
				winCount: 0,
				prizeInfo: {
					// title: '一等奖',
					// count: 0,
				}
			}
		})
		prize_info.forEach(item => {
			if (item.value == 0) {
				return;
			}
			data.prizeRank['prize' + item.value] = {
				title: item.text,
				winCount: 0,
				paramInfo: {
					// title: '初选/7列选号池/过滤2',
					// count: 0,
				}
			}
		});
		return data;
	}

	const resetBigStat = () => {
		fsDriver.remove('big-stat');
		loadData();
	}

	const loadData = () => {
		let data = getBigStat();
		paramRank.value = data.paramRank;
		prizeRank.value = data.prizeRank;
		loadCheckList();
	}

	loadData();
</script>